﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace y12fjydbs_1
{
    public partial class FrmYT : Form
    {
        #region 变量定义

        /// <summary>
        /// 焰条总数量
        /// </summary>
        const int cYTNum = 20;
        /// <summary>
        /// 命令发送次数
        /// </summary>
        int cCmdTimes = 0;
        MyButton[] button = new MyButton[cYTNum];
        List<YDStatus> clistYDStatus = new List<YDStatus>();

        private string NowTimeStr;
        readonly int cOffset = -100;

        CmdType cCmdType = CmdType.None;


        public TChState[] ChState = new TChState[cYTNum];
        int[,] btnPoint = new int[20, 2] {  { 310, 290 },{ 410, 325 },{ 470, 408 }, { 470, 511 }, { 410, 595 }, { 310, 625 }, { 210, 595 }, { 150, 511 }, { 150, 408 },{ 210, 325 },
                                            { 930, 290 },{ 1030, 325 },{ 1090, 408 }, { 1090, 511 }, { 1030, 595 }, { 930, 625 }, { 830, 595 }, { 770, 511 }, { 770, 408 },{ 830, 325 }};


        private System.Timers.Timer CmdTimer = new System.Timers.Timer();
        private System.Timers.Timer CmdTimerCX = new System.Timers.Timer();

        MySerialData cSerialData;

        bool cCheckKZQStatus = false; //查询控制器状态 

        bool cIsFristRun = true;

        delegate void SetTextCallback(string text, Color TextColor);

        bool cCountDown = false;

        int cFireTotalSenond = (int)(0.2 * 60); //焰条总时间 2分钟 * 60秒
        #endregion

        #region 构造函数
        public FrmYT()
        {
            InitializeComponent();

            cSerialData = MyGlobal.GetSerialData();
            MyGlobal.frmYT = this;
        }
        #endregion

        #region FrmYT_Load
        private void FrmYT_Load(object sender, EventArgs e)
        {
            button1.Text = "启动";
            timer1.Enabled = false;
        }
        #endregion

        #region FormClosing
        private void FrmYT_FormClosing(object sender, FormClosingEventArgs e)
        {
            cSerialData.cStop = true;
            Application.Exit();
        }
        #endregion

        int cSendCount = 1;
        #region 界面时间刷新定时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime myDateTime = DateTime.Now;

            NowTimeStr = myDateTime.ToString("    [HH:mm:ss]   ");
            label1.Text = myDateTime.ToString("yyyy年MM月dd日 HH时mm分ss秒");

            RecviveMsg($">> {cSendCount++}", Color.LightSalmon);
            cSerialData.SendCmd_CX(CmdByte.YT, 0x00, 0x00);


        }
        #endregion 

        #region 切换设备
        private void BtnTDL_Click(object sender, EventArgs e)
        {
            if (MyGlobal.frmYDL == null)
            {
                MyGlobal.frmYDL = new FrmYDL();
                MyGlobal.frmYDL.Show();
            }
            else
            {
                MyGlobal.frmYDL.Show();
            }
        }
        #endregion
         
        #region RecviveMsg
        public void RecviveMsg(string text, Color TextColor)
        {
            if (InfoDisp.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(RecviveMsg);
                InfoDisp.Invoke(d, new object[] { text, TextColor });
            }
            else
            {
                text = NowTimeStr + text + " \n";
                InfoDisp.Focus();
                //设置光标的位置到文本尾    
                InfoDisp.Select(InfoDisp.TextLength, 0);
                //滚动到控件光标处    
                InfoDisp.SelectionFont = new Font("微软雅黑", 10.5F, FontStyle.Regular);
                InfoDisp.SelectionColor = TextColor;
                InfoDisp.AppendText(text);
                InfoDisp.ScrollToCaret();
                InfoDisp.Update();
            }
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
            timer1.Enabled = true;
             
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}
